home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv3n4.zip / HELP-DIR.ZIP / HELPMAKE.C < prev    next >
Text File  |  1989-04-23  |  9KB  |  322 lines

  1. /********************** HELPMAKE.C *****************************
  2. * Author: David Michmerhuizen
  3. *
  4. * Function: Compile help screen data for help()
  5. *
  6. * Compilers:    Turbo C  V1.5
  7. *
  8. * Memory models:    all
  9. *
  10. * Usage:  HELPMAKE helpsource, [helpsource...]
  11. *
  12. *         where 'helpsource' is the filename of one or more
  13. *         help source files.
  14. *
  15. *         Helpmake then creates three files:
  16. *
  17. *               help.h       for use by application subroutines
  18. *                            that call help().
  19. *
  20. *               helpstru.h   used by help.c
  21. *
  22. *               help.hlp     data file used by help.c
  23. *
  24. ***************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30.  
  31. #ifndef TRUE
  32. #define TRUE  1
  33. #define FALSE 0
  34. #endif
  35.  
  36.  
  37. char    help_fname[]    = "help.hlp";
  38. char    struct_fname[]  = "helpstru.h";
  39. char    defs_fname[]    = "help.h";
  40.  
  41. FILE    *helpout;
  42. FILE    *structout;
  43. FILE    *defsout;
  44.  
  45. int startfiles();
  46. int endfiles( int );
  47. int makehelp( FILE *, long *, int *);
  48.  
  49.  
  50. /*--------------------------------------------------------------------------*/
  51.  
  52. void main( int argc, char *argv[])
  53. {
  54.     extern  FILE    *helpout;
  55.     extern  FILE    *structout;
  56.     extern  FILE    *defsout;
  57.     auto    FILE    *helpsrc;
  58.     auto    int     i;
  59.     auto    int     outnum = 0;
  60.     auto    long    outpos = 0L;
  61.  
  62.  
  63.     /* need at least one argument */
  64.  
  65.     if  ( argc == 1)
  66.     {
  67.         fputs("\nFormat: HELPMAKE helpsource, [helpsource...]\n\n", stdout);
  68.         exit(1);
  69.     }
  70.  
  71.     /* open output files */
  72.  
  73.     if  ( startfiles())
  74.         exit(2);
  75.  
  76.     /* process multiple input files */
  77.  
  78.     for( i=1; i<argc; i++)
  79.     {
  80.         if  ( !(helpsrc = fopen( argv[i], "rt")))
  81.         {
  82.             fputs("Error opening input file ",stdout);
  83.             fputs(argv[i],stdout);
  84.             fputc('\n',stdout);
  85.             fcloseall();
  86.             exit(3);
  87.         }
  88.         else
  89.         {
  90.             fputs( "Compiling ", stdout);
  91.             fputs( argv[i],      stdout);
  92.             fputs( "...\n",      stdout);
  93.             if  ( makehelp( helpsrc, &outpos, &outnum))
  94.             {
  95.                 fcloseall();
  96.                 exit(4);
  97.             }
  98.             else
  99.                 fclose( helpsrc);
  100.         }
  101.     }
  102.  
  103.     /* finish off output files */
  104.  
  105.     endfiles( outnum);
  106.  
  107. } /* end of main */
  108.  
  109.  
  110.  
  111.  
  112. /*--------------------------------------------------------------------------
  113. **  startfiles -- opens and puts initial output to output files
  114. */
  115.  
  116.  
  117. int startfiles()
  118. {
  119.     extern  FILE    *helpout;
  120.     extern  FILE    *structout;
  121.     extern  FILE    *defsout;
  122.     extern  char    help_fname[];
  123.     extern  char    struct_fname[];
  124.     extern  char    defs_fname[];
  125.  
  126.  
  127.     if  ( !(helpout = fopen( help_fname, "wb")))
  128.     {
  129.         fputs("Error opening output file ", stdout);
  130.         fputs( help_fname, stdout);
  131.         fputc('\n', stdout);
  132.         return(1);
  133.     }
  134.     if  ( !(structout = fopen( struct_fname, "wt")))
  135.     {
  136.         fputs("Error opening output file ", stdout);
  137.         fputs( struct_fname, stdout);
  138.         fputc('\n', stdout);
  139.         fcloseall();
  140.         return(1);
  141.     }
  142.     if  ( !(defsout = fopen( defs_fname, "wt")))
  143.     {
  144.         fputs("Error opening output file ", stdout);
  145.         fputs( defs_fname, stdout);
  146.         fputc('\n', stdout);
  147.         fcloseall();
  148.         return(1);
  149.     }
  150.  
  151.  
  152.     fputs( "/*\n",              structout);
  153.     fputs( "** helpstru.h\n",   structout);
  154.     fputs( "*/\n\n",            structout);
  155.     fputs( "HELPSCREEN helpscreens[] = {\n", structout);
  156.  
  157.     fputs( "/*\n",              defsout);
  158.     fputs( "** help.h\n",       defsout);
  159.     fputs( "*/\n\n",            defsout);
  160.  
  161.     return(0);
  162.  
  163. } /* end of startfiles */
  164.  
  165.  
  166.  
  167. /*--------------------------------------------------------------------------
  168. ** endfiles -- finishes off and closes output files
  169. */
  170.  
  171. int endfiles( int outnum)
  172. {
  173.     extern  FILE    *helpout;
  174.     extern  FILE    *structout;
  175.     extern  FILE    *defsout;
  176.  
  177.     fputs( "       };\n\n\n", structout);
  178.     fprintf( structout,  " #define HELPSCREENS %d\n\n", outnum);
  179.     fcloseall();
  180.  
  181.     return(0);
  182.  
  183. } /* end of endfiles */
  184.  
  185.  
  186.  
  187. /*--------------------------------------------------------------------------
  188. ** makehelp  -- processes an input file into output file items
  189. */
  190.  
  191. int makehelp( FILE *helpsrc, long *outpos, int *outnum)
  192. {
  193.     #define linemax 128
  194.     extern  FILE    *helpout;
  195.     static  char    delims[] = "\x20\t\n";
  196.     auto    char    *c;
  197.     auto    int     i, line = 0;
  198.     auto    char    inline[ linemax];
  199.     auto    char    outdef[ linemax];
  200.     auto    char    outnam[ linemax];
  201.     auto    int     outlen;
  202.     auto    char    tmpline[ linemax];
  203.     auto    char    *tmp;
  204.     auto    int     screen = FALSE;
  205.     auto    long    scrnpos;
  206.  
  207.  
  208.     while( fgets( inline, linemax, helpsrc))    /* while can get a line     */
  209.     {                                           /*                          */
  210.         line++;
  211.         strcpy( tmpline, inline);               /* make working copy        */
  212.         tmp = strtok( tmpline, delims);         /* get first token          */
  213.         if  ( tmp && !stricmp( tmp, ".SCREEN")) /* if .screen command       */
  214.         {
  215.             if  ( screen)                       /* if screen already active */
  216.             {
  217.                 printf(".SCREEN before .END, line %d\n", line);
  218.                 return(1);                      /* error.                   */
  219.             }
  220.             else                                /* otherwise..              */
  221.             {                                   /* start gathering info     */
  222.                 outdef[0] = outnam[0] = '\0';
  223.                 tmp = strtok( NULL, delims); /* -- first the define name */
  224.                 if  ( tmp)
  225.                     strcpy( outdef, tmp);
  226.                 while ( tmp = strtok( NULL, delims))
  227.                 {
  228.                     strcat( outnam, tmp);
  229.                     strcat( outnam, " ");
  230.                 }
  231.                 if  ( strlen( outnam))
  232.                     outnam[strlen(outnam)-1] = '\0';
  233.  
  234.                 if  ( !strlen( outdef) || !strlen( outnam))
  235.                 {
  236.                     printf("Incomplete .SCREEN def, line %d\n", line);
  237.                     return(1);
  238.                 }
  239.                 strupr( outdef);
  240.                 strlwr( outnam);
  241.                 outlen = 0;
  242.                 scrnpos = *outpos;
  243.                 screen = TRUE;
  244.             }
  245.         }                                       /* end if .screen command   */
  246.         else if  ( tmp && !stricmp( tmp, ".END")) /* else if .end command   */
  247.         {
  248.             if  ( !screen)                      /* if no screen active      */
  249.             {
  250.                 printf(".END before .SCREEN, line %d\n", line);
  251.                 return(1);                      /* error.                   */
  252.             }
  253.             else                                /* else write defs & struct */
  254.             {
  255.                 fprintf( defsout, "#define %-30s %3d\n",
  256.                     outdef, *outnum);
  257.                 fprintf( structout, "       \"%s\",%*s %8luL, %4d,\n",
  258.                     outnam, 35-strlen(outnam), " ", scrnpos, outlen);
  259.                 ++*outnum;
  260.                 *outpos += outlen;
  261.                 screen = FALSE;
  262.             }
  263.         }                                       /* end if .end command      */
  264.         else                                    /* else not a command, so   */
  265.             if  ( screen)                           /* if screen is active      */
  266.             {
  267.                 c = inline;                         /* output the line, with    */
  268.                 while( *c)                          /* repeating chars compresd */
  269.                 {
  270.                     if  ( *c == '|')                /* (except inside links)    */
  271.                     {
  272.                         do  {
  273.                             fputc( *(c++), helpout);
  274.                             outlen++;
  275.                         }
  276.                         while( *c && *c != '|');
  277.                         if  ( *c)
  278.                         {
  279.                             fputc( *(c++), helpout);
  280.                             outlen++;
  281.                         }
  282.                     }
  283.                     for ( i=1; *c == *(c+i); i++)   /* find numb of same chars  */
  284.                         ;
  285.                     if  ( i > 1)
  286.                     {
  287.                         fputc( '\xFF',   helpout);
  288.                         fputc( (char) i, helpout);
  289.                         outlen += 2;
  290.                     }
  291.                     fputc( *c, helpout);
  292.                     outlen++;
  293.                     c += i;
  294.                 }
  295.             }                                       /* end if screen active     */
  296.     }                                           /* end while                */
  297.  
  298.     return(0);
  299.  
  300. } /* end of makehelp */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.